package com.example.demo.mapper;

import com.example.demo.entity.AfshopCoupon;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 当前评价的次数表 Mapper 接口
 * </p>
 *
 * @author Mr.We
 * @since 2024-06-25
 */
@Mapper
public interface AfshopCouponMapper extends BaseMapper<AfshopCoupon> {
    /**
     * 根据优惠券ID查询优惠券信息。
     * @param couponId 优惠券ID
     * @return 返回对应的AfshopCoupon对象，如果找不到则返回null
     */
    @Select("select * from afshop_coupon where id = #{value}")
    AfshopCoupon queryObject(Integer couponId);

    /**
     * 查询优惠券列表。
     * @param map 查询条件参数，包含各种过滤条件和排序规则
     * @return 返回符合条件的优惠券列表
     */
    @Select("<script>" +
            "select * from afshop_coupon a" +
            "        where 1 = 1" +
            "        <if test='sendTypes != null'>" +
            "            and send_type in" +
            "            <foreach item=\"item\" collection=\"sendTypes\" open=\"(\" separator=\",\" close=\")\">" +
            "                #{item}" +
            "            </foreach>" +
            "        </if>" +
            "        <if test='enabled != null and enabled == true'>" +
            "            and a.use_end_date >= now()" +
            "            and now() >= a.use_start_date" +
            "        </if>" +
            "        <choose>" +
            "            <when test='sidx != null and sidx.trim() != \"\"'>" +
            "                order by ${sidx} ${order}" +
            "            </when>" +
            "            <otherwise>" +
            "                order by a.id desc" +
            "            </otherwise>" +
            "        </choose>" +
            "</script>")
    List<AfshopCoupon> queryList(Map<String, Object> map);

    /**
     * 查询满足条件的优惠券总数。
     * @param map 查询条件参数，用于统计符合条件的优惠券数量
     * @return 返回满足条件的优惠券总数
     */
    @Select("select count(*) from afshop_coupon")
    int queryTotal(Map<String, Object> map);

    /**
     * 查询用户的优惠券列表。
     * @param map 查询条件参数，包含用户ID等条件
     * @return 返回用户所有的优惠券列表
     */
    @Select("<script>" +
            "select a.*,b.coupon_number,b.user_id,b.coupon_status,b.id user_coupon_id" +
            "        from afshop_coupon a" +
            "        left join nideshop_user_coupon b on a.id = b.coupon_id" +
            "        where 1 = 1" +
            "        <if test='userId != null'>" +
            "            and b.`user_id` = #{userId}" +
            "        </if>" +
            "        <if test='sendType != null'>" +
            "            and a.send_type = #{sendType}" +
            "        </if>" +
            "        <if test='couponStatus != null'>" +
            "            and b.coupon_status = #{couponStatus}" +
            "        </if>" +
            "        <if test='couponNumber != null'>" +
            "            and b.coupon_number = #{couponNumber}" +
            "        </if>" +
            "        <if test='sourceKey != null and source_key != \"\"'>" +
            "            and b.source_key = #{sourceKey}" +
            "        </if>" +
            "        <if test='unUsed != null and unUsed == true'>" +
            "            and b.used_time is null and (b.order_id  is null or b.order_id =0)" +
            "        </if>" +
            "        <choose>" +
            "            <when test='sidx != null and sidx.trim() != \"\"'>" +
            "                order by ${sidx} ${order}" +
            "            </when>" +
            "            <otherwise>" +
            "                order by b.coupon_status asc" +
            "            </otherwise>" +
            "        </choose>" +
            "</script>")
    List<AfshopCoupon> queryUserCoupons(Map<String, Object> map);

    /**
     * 查询用户可使用的优惠券中面额最大的优惠券。
     * @param map 查询条件参数，包含用户ID等条件
     * @return 返回用户可使用的优惠券中面额最大的优惠券
     */
    @Select("<script>" +
            "select a.`id`," +
            "        a.`name`," +
            "        a.`type_money` as type_money," +
            "        a.`send_type`," +
            "        a.`min_amount`," +
            "        a.`max_amount`," +
            "        a.`send_start_date`," +
            "        a.`send_end_date`," +
            "        a.`use_start_date`," +
            "        a.`use_end_date`," +
            "        a.`min_goods_amount`," +
            "        a.min_transmit_num" +
            "        from afshop_coupon a" +
            "        left join afshop_coupon b on a.send_type = b.send_type and b.type_money > a.type_money" +
            "        where 1 = 1" +
            "        and a.use_end_date >= now()" +
            "        and now() >= a.use_start_date" +
            "        and b.id is null" +
            "        <if test='sendType != null'>" +
            "            and a.send_type = #{sendType}" +
            "        </if>" +
            "        limit 0, 1" +
            "</script>")
    AfshopCoupon queryMaxUserEnableCoupon(Map<String, Object> map);

    /**
     * 查询用户的优惠券列表。
     * @param map 查询条件参数，包含用户ID等条件
     * @return 返回用户所有的优惠券列表
     */
    @Select("select a.*,b.coupon_number,b.user_id" +
            "        from afshop_coupon a" +
            "        left join nideshop_user_coupon b on a.id = b.coupon_id" +
            "        where 1 = 1" +
            "        and a.use_end_date >= now()" +
            "        and now() >= a.use_start_date" +
            "        and a.send_type in (1,4)" +
            "        and b.user_id = #{userId} and b.order_id is null")
    List<AfshopCoupon> queryUserCouponList(Map<String, Object> map);
}
